package com.study2.demoShuangLieJiHe;

import java.util.TreeMap;

public class A05_TreeMapTestDemo {
    public static void main(String[] args) {
        /*
         * 需求：
         *   字符串 "aababcavcdabcde"
         *   请统计字符串中每一个字符出现的次数，并按照以下格式输出
         *   输出结果
         *       a(5) b(4) c(3) d(2) e(1)
         * */
        
        String str = "aababcabcdabcde";
        char[] charArray = str.toCharArray();
        TreeMap<Character, Integer> ts = new TreeMap<>();
        for (char c : charArray) {
            if (ts.containsKey(c)) {
                int cont = ts.get(c) + 1;
                ts.put(c, cont);
            } else {
                ts.put(c, 1);
            }
        }
        StringBuilder sb = new StringBuilder();
        ts.forEach((k, v) -> sb.append(k).append("(").append(v).append(")"));
        System.out.println(sb);
    }
}
